const app = getApp();
var {
  Api
} = require('../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mask: false,//书木弹框
    flage: false,
    items: [],
    id: '',//题库id
    page: 1,
    page_size: 20,
    shuid: [],//书本id
    shulist: [],//书本列表
    kong:false,
    selectd:false,
    domain: Api.domain,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    wx.setNavigationBarTitle({
      title: options.name + '错题'
    })
    this.init2()
  },
  //清空错题库
  maskkss(){
    let _this=this;
    wx.showModal({
      title: '提示',
      content: '您确定清除该错题库么？',
      success (res) {
        if (res.confirm) {
          var params = {
            course_ids: _this.data.id
          }
          console.log(params);
          
          console.log(wx.$get.wrong_log_del_1);

          wx.$http.post(wx.$get.wrong_log_del_1, params).then(res => {
            wx.showToast({
              title: '成功',
              icon: 'success',
              duration: 2000
            });
            wx.navigateTo({
              url:'/pages/myMistake/myMistake',
            })


          });
          



          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },


  mask() {
    this.setData({
      mask: true
    })
  },
  print_pdf(){
    let course_ids = [];
    let chapter_ids = [];
    let library_ids = [];
    for (const item of this.data.items) {
      if(item.flage == true){
        chapter_ids.push(item.id)
        course_ids.push(item.course_id)
        library_ids.push(item.library_id)
      }
    }
    console.log(course_ids, chapter_ids, library_ids);

    // 请求下载
    let that = this;
    const fileExtName = ".pdf";
    const randfile = new Date().getTime() + fileExtName;
    const newPath = `${wx.env.USER_DATA_PATH}/${randfile}`;
    wx.downloadFile({
      url: wx.$get.print_wrong+ '?member_id=' + wx.$cache.get('member_id') + '&library_id='+library_ids+'&chapter_id='+chapter_ids+'&course_id='+course_ids+'&disid='+ wx.getStorageSync('disidkey'),
      // formData:{
      //   member_id:wx.$cache.get('member_id'),
      //   library_id:library_ids,
      //   chapter_id:chapter_ids,
      //   course_id:course_ids
      // },
      filePath: newPath,
      success: function (res) {
      const filePath = res.tempFilePath;
      wx.openDocument({
            filePath: newPath,
            showMenu: true,
            fileType: 'pdf',
            success: function (res) {}
          })
        },
        fail: function (res) {
          wx.hideLoading();
        }
    })

  },
  sed_kuang(e){//选中
    const {index} = e.currentTarget.dataset
    this.setData({
      [`items[${index}].flage`]:!this.data.items[index].flage
    })
    let seledNum = 0
    this.data.items.forEach(el=>{
        if(el.flage){
          seledNum++
        }
    })
    if(seledNum>0){
      this.setData({
        selectd:true
      })
    }else{
      this.setData({
        selectd:false
      })
    }

  },
  gotopicAnalysis() {//去随机答题页面
    let book_ids = this.data.shuid.toString()
    let course_id = this.data.id
    wx.navigateTo({
      url: '/pages/topicAnalysis/topicAnalysis?cuo=' + true + '&book_id=' + book_ids + '&course_id=' + course_id
    })
  },
  init() {//错题章节
    let book_ids = this.data.shuid.toString()
    var that = this
    var params = {
      course_ids:  this.data.id,//科目id
      library_ids:book_ids,//题库id
      page: 1,
      page_size: this.data.page_size
    }
    wx.$http.post(wx.$get.wrong_log_chapter, params).then(res => {
      console.log('错题章节', res)
      if(res.length==0){
        this.setData({
          kong:true
        })
      }else{
        this.setData({
          kong:false
        })
      }
      this.setData({
        items: res
      })
    })
  },
  init2() {//题库列表
    var params = {
      course_ids: this.data.id
    }
    wx.$http.post(wx.$get.wrong_log_library_1, params).then(res => {
    
      console.log(res, '题库列表')
    
      let shuid = []
      function  quchong(arr){
        var result = [];
        var obj = {};
        for(var i =0; i<arr.length; i++){
           if(!obj[arr[i].key]){
              result.push(arr[i]);
              obj[arr[i].key] = true;
           }
        }
        return result
      }
      res.forEach(item=>{
        item.flage=false
      })
      for (let i = 0; i < res.length; i++) {
        shuid = shuid.concat(res[i].id)
      }
      console.log(shuid)
      this.setData({
        shulist: res,
        shuid,
      })
      this.init()
    })
  },
  pulldown() {//错题章节上拉加载
    var that = this
    this.setData({
      page: this.data.page += 1
    })
    var params = {
      course_id: this.data.id,//科目id
      page: this.data.page,
      page_size: this.data.page_size
    }
    network.requestLoading(Api.GET.question_chapter, params, 1, '', 'GET', '', function (res) { //成功的方法
      console.log('错题章节', res.list)
      let newadd = that.data.items.concat(res.list)
      if (res.list.length != 0) {
        that.setData({
          items: newadd
        })
      }
    }, function () {
    })
  },
  golistde(e) {//去章节刷题详情
    const { tid,kid,zid } = e.currentTarget.dataset
    let { num } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/brushTopic/brushTopic?tid=${tid}&kid=${kid}&zid=${zid}&num=${num}&type=3`
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.pulldown()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: network.get('fx').title,
      imageUrl: network.get('fx').img,
      path: `/pages/index/index?pid=${network.get('member_id')}`
    }
  }
})